/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if(root==nullptr){return false;}
        return dfsroute(root,sum,0);
    }
    bool dfsroute(TreeNode* root, int sum, int part_sum){
        if(root->left==nullptr && root->right==nullptr){return (part_sum+root->val)==sum;}
        if(root->left!=nullptr && root->right!=nullptr){return dfsroute(root->left,sum,part_sum+root->val) || dfsroute(root->right,sum,part_sum+root->val);}
        if(root->left!=nullptr){return dfsroute(root->left,sum,part_sum+root->val);}
        return dfsroute(root->right,sum,part_sum+root->val);
    }
};